import _thread
from time import ctime, sleep

loops = [2, 4]


def loop(nloop, sec, lock):
    print('start loop', nloop, ctime())
    sleep(sec)
    print('end loop', nloop, ctime())
    lock.release()


"""
  alocate_lock的使用
"""


def main():
    locks = []
    for i in range(len(loops)):
        lock = _thread.allocate_lock()
        lock.acquire()
        locks.append(lock)

    for i in range(len(loops)):
        _thread.start_new_thread(loop, (i, loops[i], locks[i]))

    for i in range(len(loops)):
        while locks[i].locked():
            pass


if __name__ == "__main__":
    main()
